package com.winchain.provider;

import com.winchain.provider.expiration.rx.BillExpAfterTaskSubsciber;
import com.winchain.provider.expiration.rx.BillExpBeforeTaskSubsciber;
import com.winchain.provider.expiration.rx.BillExpTaskSubsciber;
import com.winchain.provider.expiration.rx.BillExpirationEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import rx.Observable;

@RestController
public class BillExpirationExtServiceProvider {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private BillExpBeforeTaskSubsciber billExpBeforeTaskSubsciber;
	@Autowired
	private BillExpTaskSubsciber billExpTaskSubsciber;
	@Autowired
	private BillExpAfterTaskSubsciber billExpAfterTaskSubsciber;

	@RequestMapping({ "/billExpiration/expire" })
	public void expire(@RequestParam("date") final String date, @RequestParam("pm") final boolean pm) {
		new Thread(new Runnable() {
			public void run() {
				BillExpirationExtServiceProvider.this.logger.info("--------BillExpBatchJob excute start--------");
				BillExpirationExtServiceProvider.this.logger.info("--------excute date:" + date + "--------PM:" + pm);
				BillExpirationEvent event = new BillExpirationEvent();
				event.setDate(date);
				event.setPm(true);

				Observable<BillExpirationEvent> observers = Observable.just(event);
				observers.subscribe(BillExpirationExtServiceProvider.this.billExpBeforeTaskSubsciber);
				observers.subscribe(BillExpirationExtServiceProvider.this.billExpTaskSubsciber);
				observers.subscribe(BillExpirationExtServiceProvider.this.billExpAfterTaskSubsciber);

				BillExpirationExtServiceProvider.this.logger.info("--------BillExpBatchJob excute end--------");
			}
		})

				.start();
	}
}
